package org.xxpay.pay.channel.you;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.xxpay.common.http.HttpRequestTemplate;
import org.xxpay.common.util.HtmlUtils;
import org.xxpay.common.util.sign.SignUtils;
import org.xxpay.common.util.str.StringUtils;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.util.AmountUtil;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.pay.channel.BasePayment;

import java.util.Map;

@Service
public class YouPaymentService extends BasePayment {

    private final static String logPrefix = "【优卡付】";
    @Override
    public String getChannelName() { return YouConfig.CHANNEL_NAME; }

    @Override
    public JSONObject pay(PayOrder payOrder) {
        return doOrderReq(payOrder);
    }

    /**
     * 查询订单
     * @param payOrder
     * @return
     */
    @Override
    public JSONObject query(PayOrder payOrder) {
        JSONObject retObj = buildRetObj();
        retObj.put(PayConstant.RETURN_PARAM_RETCODE, PayConstant.RETURN_VALUE_FAIL);
        retObj.put(PayConstant.RESULT_PARAM_ERRDES, "没有订单查询接口");
        return retObj;
    }

    //下单请求实现
    private JSONObject doOrderReq(PayOrder payOrder){
        YouConfig config = new YouConfig(getPayParam(payOrder));
        Map<String,Object> params = Maps.newHashMap();
        //设置请求参数
        setParames(payOrder, config, params);
        _log.info("{}请求数据:{}", getChannelName(), JSON.toJSONString(params));
        //将订单改为支付中
        rpcCommonService.rpcPayOrderService.updateStatus4Ing(payOrder.getPayOrderId(), null);
        String payForm = HtmlUtils.form(config.getRequestUrl()+YouConfig.REQ_PAY_URL_FIXX, HtmlUtils.POST, params);
        JSONObject retObj = new JSONObject();
        return buildPayResultOfForm(retObj, payOrder, payForm);
    }
    //设置请求参数
    private void setParames(PayOrder payOrder,YouConfig config,  Map<String,Object> parameters){
        parameters.put("version", "1.0");
        parameters.put("customerid", config.getAppId());
        parameters.put("sdorderno", payOrder.getPayOrderId());
        parameters.put("total_fee", AmountUtil.convertCent2Dollar(String.valueOf(payOrder.getAmount())));
        parameters.put("paytype", config.getPayMode());
        parameters.put("notifyurl", payConfig.getNotifyUrl(getChannelName()));
        parameters.put("returnurl", payOrder.getReturnUrl());
        parameters.put("sign", getSign(parameters, config.getPrivateKey()));
    }

    //获取sign
    private String getSign(Map<String,Object> params,String key){
        String signTxt = "version="+params.get("version").toString()
                +"&customerid=" +params.get("customerid").toString()
                +"&total_fee="+params.get("total_fee").toString()
                +"&sdorderno="+params.get("sdorderno").toString()
                +"&notifyurl="+params.get("notifyurl").toString()
                +"&returnurl="+params.get("returnurl").toString();
        String sign = SignUtils.MD5.createSign(  signTxt, "&"+key  , SignUtils.CHARSET_UTF8);
        _log.info("{}待签名字符串：{}", logPrefix, signTxt+"&"+key);
        _log.info("{}签名结果：{}", logPrefix, sign);
        return sign;
    }
}

